package com.dkd.flow.service.impl;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dkd.common.utils.DateUtils;
import com.dkd.flow.domain.EProcessDefinition;
import com.dkd.flow.domain.EProcessInstances;
import com.dkd.flow.domain.EProcessTask;
import com.dkd.flow.emums.FlowOperation;
import com.dkd.flow.emums.TaskStatus;
import com.dkd.flow.emums.TaskType;
import com.dkd.flow.mapper.EProcessTaskMapper;
import com.dkd.flow.service.IEProcessDefinitionService;
import com.dkd.flow.service.IEProcessInstancesService;
import com.dkd.flow.service.IEProcessTaskService;
import com.dkd.flow.utils.FlowOperationUtils;
import com.dkd.flow.utils.NodeUtils;
import com.dkd.flow.utils.RevokeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
/**
 * 流程任务Service业务层处理
 *
 * @author itheima
 * @date 2025-02-27
 */
@Service
public class EProcessTaskServiceImpl extends ServiceImpl<EProcessTaskMapper, EProcessTask> implements IEProcessTaskService {
    @Autowired
    FlowOperationUtils flowOperationUtils;
    @Autowired
    RevokeUtils revokeUtils;
    @Autowired
    private NodeUtils nodeUtils;
    @Autowired
    private IEProcessDefinitionService ieProcessDefinitionService;
    @Autowired
    private IEProcessInstancesService ieProcessInstancesService;
    @Autowired
    private EProcessTaskMapper eProcessTaskMapper;
    /**
     * 查询流程任务
     *
     * @param ID 流程任务主键
     * @return 流程任务
     */
    @Override
    public EProcessTask selectEProcessTaskByID(Long ID) {
        return eProcessTaskMapper.selectEProcessTaskByID(ID);
    }
    /**
     * 查询流程任务列表
     *
     * @param eProcessTask 流程任务
     * @return 流程任务
     */
    @Override
    public List<EProcessTask> selectEProcessTaskList(EProcessTask eProcessTask) {
        return eProcessTaskMapper.selectEProcessTaskList(eProcessTask);
    }
    /**
     * 新增流程任务
     *
     * @param eProcessTask 流程任务
     * @return 结果
     */
    @Override
    public int insertEProcessTask(EProcessTask eProcessTask) {
        eProcessTask.setCreateTime(DateUtils.getNowDate());
        return eProcessTaskMapper.insertEProcessTask(eProcessTask);
    }
    /**
     * 修改流程任务
     *
     * @param eProcessTask 流程任务
     * @return 结果
     */
    @Override
    public int updateEProcessTask(EProcessTask eProcessTask) {
        eProcessTask.setUpdateTime(DateUtils.getNowDate());
        return eProcessTaskMapper.updateEProcessTask(eProcessTask);
    }
    /**
     * 批量删除流程任务
     *
     * @param IDs 需要删除的流程任务主键
     * @return 结果
     */
    @Override
    public int deleteEProcessTaskByIDs(Long[] IDs) {
        return eProcessTaskMapper.deleteEProcessTaskByIDs(IDs);
    }
    /**
     * 删除流程任务信息
     *
     * @param ID 流程任务主键
     * @return 结果
     */
    @Override
    public int deleteEProcessTaskByID(Long ID) {
        return eProcessTaskMapper.deleteEProcessTaskByID(ID);
    }
    @Override
    public EProcessTask getTaskDetail(JSONObject data) {
        Long curTaskId = data.getLong("curTaskId");
        Long processInstancesId = data.getLong("processInstancesId");
        Long processDefinitionId = data.getLong("processDefinitionId");
        EProcessTask eProcessTask = null;
        EProcessInstances eProcessInstances = null;
        //允许的操作
        List<Integer> flowOperationBtnShow = new ArrayList<>();
        JSONObject relations = null;
        //字段值
        JSONObject formValue = null;
        if (curTaskId != null) {
            //获取流程任务信息
            eProcessTask = getById(curTaskId);
            processInstancesId = eProcessTask.getProcessInstancesId();
            //流程实例信息
            eProcessInstances = ieProcessInstancesService.getById(processInstancesId);
            //流程状态
            eProcessTask.setFlowStatus(eProcessInstances.getFlowStatus());
            processDefinitionId = eProcessInstances.getProcessDefinitionId();
            //字段值
            formValue = JSONObject.parseObject(eProcessInstances.getFormValue());
            Long taskType = eProcessTask.getTaskType();
            Long taskStatus = eProcessTask.getTaskStatus();
            flowOperationBtnShow = flowOperationUtils.getFlowOperationBtnShow(taskStatus,
                    taskType,
                    curTaskId);
        }
        //发起流程
        else {
            eProcessTask = new EProcessTask();
            if (processDefinitionId != null) {
                eProcessTask.setProcessDefinitionId(processDefinitionId);
            }
            else {
                throw new RuntimeException("流程定义ID不能为空");
            }
            flowOperationBtnShow = flowOperationUtils.getFlowOperationBtnShow(null,
                    TaskType.INITIATOR_TASK.getValue().longValue(),
                    null);
        }
        eProcessTask.setFlowOperationBtnShow(flowOperationBtnShow);
        //节点信息
        relations = nodeUtils.initRelationsByData(processDefinitionId);
        //节点
        ArrayList<JSONObject> nodes = (ArrayList<JSONObject>) relations.get("nodes");
        //表单定义
        EProcessDefinition eProcessDefinition = ieProcessDefinitionService.getById(eProcessTask.getProcessDefinitionId());
        //字段结构
        JSONObject json = JSONObject.parseObject(eProcessDefinition.getJson());
        String processDesignId = eProcessTask.getProcessDesignId();
        JSONObject curNodeProcessDesign = null;
        if (processDesignId != null) {
            //当前节点
            curNodeProcessDesign = nodes.stream().filter(item -> item.getString("key").equals(processDesignId)).findFirst().get();
            json.put("curNodeProcessDesign",
                    curNodeProcessDesign.getJSONObject("node"));
        }
        //发起节点
        else {
            curNodeProcessDesign = nodes.get(0);
            json.put("curNodeProcessDesign",
                    curNodeProcessDesign.getJSONObject("node"));
        }
        json.put("curNodeProcessDesign",
                curNodeProcessDesign.getJSONObject("node"));
        json.put("formValue",
                formValue);
        eProcessTask.setJson(json.toJSONString());
        return eProcessTask;
    }
}
